﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Text;

namespace Cms.Web.UIBuilder
{
    public partial class ParseXmlForm : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string param = EntLib.Request.GetString("param");
                string formid = EntLib.Request.GetString("id");
                if (!String.IsNullOrEmpty(param) && !String.IsNullOrEmpty(formid))
                {
                    string url = "";
                    BLL.ParseXmlForm pxf = null;
                    try
                    {
                        if (param == "model")
                        {
                            BLL.Sys_Selfform_Layout bll = new Cms.BLL.Sys_Selfform_Layout();

                            Model.Sys_Selfform_Layout model = bll.GetById(formid, false);
                            url = model.Xmldata;// filename;//Server.MapPath(String.Format("~/CmsSystem/SysModules/{0}",filename));
                            pxf = new BLL.ParseXmlForm(url); 
                        }
                        else if (param == "list")
                        {
                            BLL.Sys_Selffom_List_Layout bll = new Cms.BLL.Sys_Selffom_List_Layout();
                            Model.Sys_Selfform_List_Layout model = bll.GetById(formid, false);
                            url = model.Xmldata;
                            pxf = new BLL.ParseXmlForm(url,Cms.BLL.XmlFormType.Query,formid);
                        }
                    }
                    catch (Exception ex)
                    {
                        url = "";
                        pxf = null;
                    }
                    if (pxf == null)
                        return;
                     
                    string html = pxf.Parse();
                    dhtml.InnerHtml = html;
                    if (pxf.Requires!=null && pxf.Requires.Count > 0)
                    {
                        System.Web.UI.WebControls.Literal li = new Literal();
                        StringBuilder sb = new StringBuilder();
                        foreach (string str in pxf.Requires)
                        {
                            switch (str.ToLower())
                            {
                                case "dock":
                                    sb.AppendLine("<script src=\"/Scripts/jquery.layoutengine.js\" type=\"text/javascript\"></script>");
                                    break;
                                case "treegrid":
                                    sb.AppendLine("<script src=\"/Scripts/jqGrid/lan/grid.locale-cn.js\" type=\"text/javascript\"></script>");
                                    sb.AppendLine(" <link href=\"/App_Themes/Default/ui.jqgrid.css\" rel=\"stylesheet\" type=\"text/css\" />");
                                    sb.AppendLine("<link href=\"/App_Themes/Default/jqueryui/jquery-ui-1.7.1.custom.css\" rel=\"stylesheet\" type=\"text/css\" />");
                                    sb.AppendLine("<script src=\"/Scripts/jqGrid/jquery.jqGrid.min.js\" type=\"text/javascript\"></script>");
                                    //sb.AppendLine("<link href=\"/App_Themes/Default/Flexigrid/flexigrid.css\" rel=\"stylesheet\" type=\"text/css\" />");
                                    //sb.AppendLine("<script src=\"/Scripts/flexigrid.js\" type=\"text/javascript\"></script>");
                                    break;
                                case "richeditor":
                                    sb.AppendLine("<script src=\"/Scripts/kindeditor/kindeditor-min.js\" type=\"text/javascript\"></script>");
                                    sb.AppendLine("<script src=\"/Scripts/kindeditor/lang/zh-CN.js\" type=\"text/javascript\"></script>");
                                    break;
                                default:
                                    break;
                            }
                        }
                        sb.AppendFormat("<script type='text/javascript'>var Requires=['{0}']</script>", string.Join("','", pxf.Requires.ToArray()));
                        li.Text = sb.ToString();
                        this.GlabHeader.Controls.Add(li);
                    }
                    if (pxf.InitScripts.Count > 0)
                    {
                        System.Web.UI.WebControls.Literal li = new Literal();
                        StringBuilder sb = new StringBuilder();
                        sb.AppendLine("<script type='text/javascript'>");
                        foreach (string str in pxf.InitScripts)
                        {
                            sb.AppendLine(str);
                        }
                        sb.AppendLine("</script>");
                        li.Text = sb.ToString();
                        this.GlabHeader.Controls.Add(li);
                    }

                }
            }
        }
    }
}
